/* EasyWay Game Engine
 * Copyright (C) 2006 Daniele Paggi.
 *  
 * Written by: 2006 Daniele Paggi<dshnt@hotmail.com>
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/**
 * 
 */
package org.easyway.editor.forms.southPanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.List;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import org.easyway.editor.commands.Command;
import org.easyway.editor.forms.EditorCore;
import org.easyway.objects.Camera;
import org.easyway.system.StaticRef;

/**
 * @author Proprietario
 * 
 */
public class SouthOptionPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private JTabbedPane tabbedPane = null;

	private JPanel cameraPanel = null;

	private JLabel jLabel = null;

	private JLabel xPosition = null;

	private JLabel yPosition = null;

	private JLabel cameraY = null;

	private JLabel cameraX = null;

	private JLabel jLabel1 = null;

	private JLabel cameraZoom = null;

	private JButton resetCameraZoom = null;

	private JLabel jLabel2 = null;

	private JLabel jLabel3 = null;

	private JLabel jLabel4 = null;

	private JTextField widthCameraGrid = null;

	private JTextField heightCameraGrid = null;

	/**
	 * This is the default constructor
	 */
	public SouthOptionPanel() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.fill = GridBagConstraints.BOTH;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.weighty = 1.0;
		gridBagConstraints.anchor = GridBagConstraints.CENTER;
		gridBagConstraints.gridx = 0;
		this.setSize(930, 82);
		this.setLayout(new GridBagLayout());
		this.setPreferredSize(new Dimension(930, 82));
		this.setBackground(new Color(238, 238, 238));
		this.addComponentListener(new java.awt.event.ComponentAdapter() {
			public void componentHidden(java.awt.event.ComponentEvent e) {
				if (thread != null) {
					thread.loop = false;
					thread = null;
				}
			}

			public void componentShown(java.awt.event.ComponentEvent e) {
				if (thread == null) {
					(thread = new CameraThread()).start();
				}
			}
		});
		setVisible(false);
		this.add(getTabbedPane(), gridBagConstraints);
	}

	/**
	 * This method initializes tabbedPane
	 * 
	 * @return javax.swing.JTabbedPane
	 */
	private JTabbedPane getTabbedPane() {
		if (tabbedPane == null) {
			tabbedPane = new JTabbedPane();
			tabbedPane.addTab("Camera", null, getCameraPanel(), null);
			tabbedPane.addTab("TiledMap", null, getTiledMapPanel(), null);
			tabbedPane.addTab("Texture", null, getTextureManagerPanel(), null);
		}
		return tabbedPane;
	}

	/**
	 * This method initializes cameraPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getCameraPanel() {
		if (cameraPanel == null) {
			jLabel4 = new JLabel();
			jLabel4.setBounds(new Rectangle(191, 35, 38, 16));
			jLabel4.setText("Height");
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(191, 18, 38, 16));
			jLabel3.setText("Width");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(191, 1, 86, 16));
			jLabel2.setText("Grid Size");
			cameraZoom = new JLabel();
			cameraZoom.setBounds(new Rectangle(144, 1, 38, 16));
			cameraZoom.setText("0");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(102, 1, 38, 16));
			jLabel1.setText("Zoom:");
			cameraX = new JLabel();
			cameraX.setBounds(new Rectangle(37, 18, 57, 16));
			cameraX.setText("0");
			cameraY = new JLabel();
			cameraY.setBounds(new Rectangle(37, 35, 57, 16));
			cameraY.setText("0");
			yPosition = new JLabel();
			yPosition.setBounds(new Rectangle(7, 35, 24, 16));
			yPosition.setText("Y");
			xPosition = new JLabel();
			xPosition.setBounds(new Rectangle(7, 18, 24, 16));
			xPosition.setText("X");
			jLabel = new JLabel();
			jLabel.setText("Position");
			jLabel.setBounds(new Rectangle(7, 1, 86, 16));
			cameraPanel = new JPanel();
			cameraPanel.setLayout(null);
			cameraPanel.add(jLabel, null);
			cameraPanel.add(xPosition, null);
			cameraPanel.add(yPosition, null);
			cameraPanel.add(cameraY, null);
			cameraPanel.add(cameraX, null);
			cameraPanel.add(jLabel1, null);
			cameraPanel.add(cameraZoom, null);
			cameraPanel.add(getResetCameraZoom(), null);
			cameraPanel.add(jLabel2, null);
			cameraPanel.add(jLabel3, null);
			cameraPanel.add(jLabel4, null);
			cameraPanel.add(getWidthCameraGrid(), null);
			cameraPanel.add(getHeightCameraGrid(), null);

			cameraPanel.add(getSnapToGrid(), null);
			cameraPanel.add(getLayerList(), null);
		}
		return cameraPanel;
	}

	/**
	 * This method initializes resetCameraZoom
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getResetCameraZoom() {
		if (resetCameraZoom == null) {
			resetCameraZoom = new JButton();
			resetCameraZoom.setBounds(new Rectangle(97, 23, 91, 21));
			resetCameraZoom.setFont(new Font("Arial", Font.PLAIN, 10));
			resetCameraZoom.setHorizontalTextPosition(SwingConstants.LEADING);
			resetCameraZoom.setHorizontalAlignment(SwingConstants.LEADING);
			resetCameraZoom.setText("Reset Zoom");
			resetCameraZoom
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							final Camera camera = StaticRef.getCamera();
							if (camera == null)
								return;
							new Command() {

								public void creation() {
									camera.zoom2D(1.0f);
									destroy();
								}

								public void loop() {
								}

								public void render() {
								}

							};
						}
					});
		}
		return resetCameraZoom;
	}

	int oldWidth = 100;

	/**
	 * This method initializes widthCameraGrid
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getWidthCameraGrid() {
		if (widthCameraGrid == null) {
			widthCameraGrid = new JTextField();
			widthCameraGrid.setBounds(new Rectangle(231, 18, 46, 16));
			widthCameraGrid.setBackground(Color.green);
			widthCameraGrid.setText("100");
			widthCameraGrid
					.addCaretListener(new javax.swing.event.CaretListener() {
						public void caretUpdate(javax.swing.event.CaretEvent e) {
							int newWidth = oldWidth;
							try {
								newWidth = Integer.parseInt(widthCameraGrid
										.getText());
								if (newWidth > 9) {
									EditorCore.setWidthGrid(newWidth);
									widthCameraGrid.setBackground(Color.GREEN);
								} else {
									widthCameraGrid.setBackground(Color.PINK);
								}
							} catch (Exception ex) {
								// newWidth = oldWidth;
								// widthCameraGrid.setText(""+oldWidth);
								widthCameraGrid.setBackground(Color.PINK);
							}

						}
					});
		}
		return widthCameraGrid;
	}

	int oldHeight = 100;

	/**
	 * This method initializes heightCameraGrid
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getHeightCameraGrid() {
		if (heightCameraGrid == null) {
			heightCameraGrid = new JTextField();
			heightCameraGrid.setBounds(new Rectangle(231, 35, 46, 16));
			heightCameraGrid.setBackground(Color.green);
			heightCameraGrid.setText("100");
			heightCameraGrid
					.addCaretListener(new javax.swing.event.CaretListener() {
						public void caretUpdate(javax.swing.event.CaretEvent e) {
							int newHeight = oldHeight;
							try {
								newHeight = Integer.parseInt(heightCameraGrid
										.getText());
								if (newHeight > 9) {
									EditorCore.setHeightGrid(newHeight);
									heightCameraGrid.setBackground(Color.GREEN);
								} else {
									heightCameraGrid.setBackground(Color.PINK);
								}
							} catch (Exception ex) {
								// newHeight = oldWidth;
								// heightCameraGrid.setText("" + oldWidth);
								heightCameraGrid.setBackground(Color.PINK);
							}
						}
					});
		}
		return heightCameraGrid;
	}

	public CameraThread thread;

	private static JCheckBox snapToGrid = null;

	private TiledMapPanel tiledMapPanel = null;

	private List layerList = null;

	private TextureManagerPanel textureManagerPanel = null;

	class CameraThread extends Thread {

		public boolean loop = true;

		public void run() {
			Camera camera;
			do {
				try {
					Thread.sleep(200L); // 5 FPS
				} catch (InterruptedException e) {
				}
				camera = StaticRef.getCamera();
				if (camera == null)
					continue;
				cameraX.setText("" + camera.getx());
				cameraY.setText("" + camera.gety());
				cameraZoom.setText("" + camera.getZoom2D());
			} while (loop);
		}
	}

	/**
	 * This method initializes snapToGrid
	 * 
	 * @return javax.swing.JCheckBox
	 */
	public static JCheckBox getSnapToGrid() {
		if (snapToGrid == null) {
			snapToGrid = new JCheckBox();
			snapToGrid.setBounds(new Rectangle(281, 1, 113, 16));
			snapToGrid.setText("Snap To Grid");
		}
		return snapToGrid;
	}

	/**
	 * This method initializes tiledMapPanel
	 * 
	 * @return org.easyway.editor.forms.southPanel.TiledMapPanel
	 */
	private TiledMapPanel getTiledMapPanel() {
		if (tiledMapPanel == null) {
			tiledMapPanel = new TiledMapPanel();
		}
		return tiledMapPanel;
	}

	// @jve:decl-index=0:

	/**
	 * This method initializes layerList
	 * 
	 * @return java.awt.List
	 */
	private List getLayerList() {
		if (layerList == null) {
			layerList = new List();
			layerList.setBounds(new Rectangle(406, 1, 128, 58));
			layerList.setBackground(new Color(255, 255, 204));
			layerList.setMultipleMode(true);
			initList();
			layerList.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					int number = 0;
					String txt = null;
					try {
						number = (Integer) e.getItem();
						// System.out.println(">>>>>>>>>>>>>>>>> TESTO : "+txt);
						// txt = txt.substring("layer n. ".length());
						// System.out.println(txt);
						// number = Integer.parseInt(txt);
					} catch (Exception ee) {
						ee.printStackTrace();
						System.out.println("bug found; txt = " + txt);
						return;
					}
					if (e.getStateChange() == e.DESELECTED) {
						EditorCore.showLayer[number] = false;
					} else {
						EditorCore.showLayer[number] = true;
					}
				}
			});
		}

		return layerList;
	}

	protected void initList() {
		for (int i = 0; i < EditorCore.numberOfLayers; ++i) {
			layerList.add("layer n. " + i);
			layerList.select(i);
		}

	}

	/**
	 * This method initializes textureManagerPanel	
	 * 	
	 * @return org.easyway.editor.forms.southPanel.TextureManagerPanel	
	 */
	private TextureManagerPanel getTextureManagerPanel() {
		if (textureManagerPanel == null) {
			textureManagerPanel = new TextureManagerPanel();
		}
		return textureManagerPanel;
	}

}
